(function(){
	
	function chartTool(){
		this.pieSetting = {
		container:'',//要显示的div或其他元素的ID
		url:'',//请求地址
		parameter:{},//参数
		title:'',//title
		titleStyle:{color:'#000000'},
		
		subtitle:'',//副标
		subtitleStyle:{color:'#000000'},
		type:'',//图表类型 pie,column,line
		showInLegend:true,//饼图 是否显示图例
		showLabel:true,//饼图 是否显示图例
		labelColor:'#000',
		labelLineColor:'#000',
		numberfix:2,//四舍五入保留的小数位数
		seriesName:'所占比例',
		labelStyle:'percentage',//label提示内容：原值(value)、比例(percentage)、
		yAxis:'纵坐标值'
		}
	}
	
	

	var a = chartTool.prototype;
	
	window.ChartTool = new chartTool();
	
	
	a.init = function(options){
			
			var setting = $.extend(a.pieSetting,options);
			
			if(setting.container===''||setting.url===''||setting.type===''){
				alert('参数不足');
			}else{
				
				$.ajax({
					type:'post',
					url:setting.url,
					dataType:'json',
					data:setting.parameter,
					success:function(result){
						if('pie'===setting.type){//饼图
							a.pieChart(setting,result.data);
						}else{//曲线和柱状
							a.initLine(setting,result);
						}
					},
					error:function(result){
						alert("请求错误");
						return;
					}
				});
			}
			
		}
	
	a.pieChart = function(setting,data){
		
			var chartp = new Highcharts.Chart({
			chart: {
				renderTo: setting.container,
				plotBackgroundColor: null,
				plotBorderWidth: null,
				plotShadow: false
			},
			title: {
				text: setting.title,
				style:setting.titleStyle
			},
			subtitle: {
				  text: setting.subtitle,
				  style:setting.subtitleStyle
				 },
			tooltip: {
				pointFormat: '{series.name}:{point.percentage}%',
				percentageDecimals: 1
			},
			plotOptions: {
				pie: {
					allowPointSelect: true,
					cursor: 'pointer',
					dataLabels: {
						enabled: setting.showLabel,
						color: setting.labelColor,
						connectorColor: setting.labelLineColor,
						formatter: function() {//格式化标签提示内容
						  if('percentage'===setting.labelStyle){
							  return  this.point.name +':'+ this.percentage.toFixed(setting.numberfix) +' %';
						  }else if('value'===setting.labelStyle){
							  return  this.point.name +':'+ this.y;
						  }
						}
					},
					showInLegend:setting.showInLegend
				}
			},
			credits:{text:''},
			series: [{
				type: setting.type,
				name: setting.seriesName,
				data:data
			}]
		});
		return chartp;
		};
		
	a.initLine = function(setting,data){
		
			
		var xAxis=data.xAxis;//横轴
		var chartx = new Highcharts.Chart({
	         chart: {
	             renderTo: setting.container,
	             type: setting.type,
	             marginRight: 130,
	             marginBottom: 25
	         },
	         title: {
	             text: setting.title,
	             style:setting.titleStyle,
	             x: -20 //center
	         },
	         subtitle: {
	             text: setting.subTitle,
	             style:setting.subtitleStyle,
	             x: -20
	         },
	         xAxis: {
	             categories: xAxis
	         },
	         yAxis: {
	             title: {
	                 text: setting.yAxis
	             },
	             plotLines: [{
	                 value: 0,
	                 width: 1,
	                 color: '#808080'
	             }]
	         },credits:{text:''},
	         tooltip: {
	             formatter: function() {
//	            	 if('percentage'===setting.labelStyle){
//                 		  return  this.point.name +':'+ this.percentage.toFixed(setting.numberfix) +' %';
//                 	  }else if('value'===setting.labelStyle){
//                 		  return  this.point.name +':'+ this.y;
//                 	  }
	            	 
	                     return '<b>'+ this.series.name +'</b><br/>'+
	                     this.x +': '+ this.y +'°C';
	             }
	         },
	         legend: {
	             layout: 'vertical',
	             align: 'right',
	             verticalAlign: 'top',
	             x: -10,
	             y: 100,
	             borderWidth: 0
	         },
	         series: data.data
	     });
		return chartx;

		};
	
	})();

